using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using <Application>.<Business>;

namespace <Application>.<Data>
{
    public class <OwnerU>_<TableU>DataClass
    {
        public static DataTable SelectAll()
        {
            using (var context = new <Database>DBContext())
            {
<If Loop>
      <IsFKeyCount>
            var Query = (from _<OwnerU>_<TableU> in context.<OwnerU>_<TableU>
     	<ExitLoop>
</If Loop>
<If Loop>
     <IsFKeyCount>
     <And>
     <IsRelated>
     <And>
     <IsMiscTag>
                        join A<MiscTag>_<ROwner>_<RTable> in context.<ROwner>_<RTable> on _<OwnerU>_<TableU>.<Field> equals A<MiscTag>_<ROwner>_<RTable>.<RField>
     <IsFKeyCount>
     <And>
     <IsRelated>
     <And>
     <IsNotMiscTag>
                        join _<ROwner>_<RTable> in context.<ROwner>_<RTable> on _<OwnerU>_<TableU>.<Field> equals _<ROwner>_<RTable>.<RField>
</If Loop>
<If Loop>
      <IsFKeyCount>
                        select new <OwnerU>_<TableU>Joined() {
     	<ExitLoop>
</If Loop>
<If Loop>
     <IsFKeyCount>
     <And>
     <IsRelated>
     <And>
     <IsMiscTag>
     <And>
     <IsCurRow=[0]>
                            <Field> = A<MiscTag>_<ROwner>_<RTable>.<RDisplayFieldU>
     <IsFKeyCount>
     <And>
     <IsRelated>
     <And>
     <IsMiscTag>
     <And>
     <IsCurRow<>[0]>
                           ,<Field> = A<MiscTag>_<ROwner>_<RTable>.<RDisplayFieldU>
     <IsFKeyCount>
     <And>
     <IsRelated>
     <And>
     <IsNotMiscTag>
     <And>
     <IsCurRow=[0]>
                            <Field> = _<ROwner>_<RTable>.<RDisplayFieldU>
     <IsFKeyCount>
     <And>
     <IsRelated>
     <And>
     <IsNotMiscTag>
     <And>
     <IsCurRow<>[0]>
                           ,<Field> = _<ROwner>_<RTable>.<RDisplayFieldU>
     <IsFKeyCount>
     <And>
     <IsNotRelated>
     <And>
     <IsCurRow=[0]>
                            <Field> = _<OwnerU>_<TableU>.<Field>
     <IsFKeyCount>
     <And>
     <IsNotRelated>
     <And>
     <IsCurRow<>[0]>
                           ,<Field> = _<OwnerU>_<TableU>.<Field>
</If Loop>
<If Loop>
      <IsFKeyCount>
                        }).AsQueryable();

                return Query.ToList().ToDataTable<<OwnerU>_<TableU>Joined>();
     	<ExitLoop>
</If Loop>
<If Loop>
      <IsNotFKeyCount>
                return context.<OwnerU>_<TableU>.ToList().ToDataTable<<OwnerU>_<TableU>>();
     	<ExitLoop>
</If Loop>
            }
        }

        public static DataTable Select_Records(<DRType> <DRField>)
        {
            using (var context = new <Database>DBContext())
            {
<If Loop>
      <IsFKeyCount>
            var Query = (from _<OwnerU>_<TableU> in context.<OwnerU>_<TableU>
     	<ExitLoop>
</If Loop>
<If Loop>
     <IsFKeyCount>
     <And>
     <IsRelated>
     <And>
     <IsMiscTag>
                        join A<MiscTag>_<ROwner>_<RTable> in context.<ROwner>_<RTable> on _<OwnerU>_<TableU>.<Field> equals A<MiscTag>_<ROwner>_<RTable>.<RField>
     <IsFKeyCount>
     <And>
     <IsRelated>
     <And>
     <IsNotMiscTag>
                        join _<ROwner>_<RTable> in context.<ROwner>_<RTable> on _<OwnerU>_<TableU>.<Field> equals _<ROwner>_<RTable>.<RField>
</If Loop>
                        where _<DOwner>_<DTable>.<DRField> == <DRField>
<If Loop>
      <IsFKeyCount>
                        select new <OwnerU>_<TableU>Joined() {
     	<ExitLoop>
</If Loop>
<If Loop>
     <IsFKeyCount>
     <And>
     <IsRelated>
     <And>
     <IsMiscTag>
     <And>
     <IsCurRow=[0]>
                            <Field> = A<MiscTag>_<ROwner>_<RTable>.<RDisplayFieldU>
     <IsFKeyCount>
     <And>
     <IsRelated>
     <And>
     <IsMiscTag>
     <And>
     <IsCurRow<>[0]>
                           ,<Field> = A<MiscTag>_<ROwner>_<RTable>.<RDisplayFieldU>
     <IsFKeyCount>
     <And>
     <IsRelated>
     <And>
     <IsNotMiscTag>
     <And>
     <IsCurRow=[0]>
                            <Field> = _<ROwner>_<RTable>.<RDisplayFieldU>
     <IsFKeyCount>
     <And>
     <IsRelated>
     <And>
     <IsNotMiscTag>
     <And>
     <IsCurRow<>[0]>
                           ,<Field> = _<ROwner>_<RTable>.<RDisplayFieldU>
     <IsFKeyCount>
     <And>
     <IsNotRelated>
     <And>
     <IsCurRow=[0]>
                            <Field> = _<OwnerU>_<TableU>.<Field>
     <IsFKeyCount>
     <And>
     <IsNotRelated>
     <And>
     <IsCurRow<>[0]>
                           ,<Field> = _<OwnerU>_<TableU>.<Field>
</If Loop>
<If Loop>
      <IsFKeyCount>
                        }).AsQueryable();

                return Query.ToList().ToDataTable<<OwnerU>_<TableU>Joined>();
     	<ExitLoop>
</If Loop>
<If Loop>
      <IsNotFKeyCount>
                return context.<OwnerU>_<TableU>.ToList().ToDataTable<<OwnerU>_<TableU>>();
     	<ExitLoop>
</If Loop>
            }
        }

        public static <OwnerU>_<TableU> Select_Record(<OwnerU>_<TableU> _<OwnerU>_<TableU>)
        {
            using (var context = new <Database>DBContext())
            {
                return context.<OwnerU>_<TableU>.Find(
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                                                 _<OwnerU>_<TableU>.<FieldU>
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                                                , _<OwnerU>_<TableU>.<FieldU>
    </If Loop>
                                            );
            }
        }

        public static bool Add(<OwnerU>_<TableU> _<OwnerU>_<TableU>)
        {
            using (var context = new <Database>DBContext())
            {
                context.<OwnerU>_<TableU>.Add(_<OwnerU>_<TableU>);
                return context.SaveChanges() > 0;
            }
        }

        public static bool Update(<OwnerU>_<TableU> _<OwnerU>_<TableU>)
        {
            using (var context = new <Database>DBContext())
            {    
                context.<OwnerU>_<TableU>.Attach(_<OwnerU>_<TableU>);
                context.Entry(_<OwnerU>_<TableU>).State = System.Data.Entity.EntityState.Modified;
                return context.SaveChanges() > 0;
            }
        }

        public static bool Delete(<OwnerU>_<TableU> _<OwnerU>_<TableU>)
        {
            using (var context = new <Database>DBContext())
            {
                context.<OwnerU>_<TableU>.Remove(context.<OwnerU>_<TableU>.Find(
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                                                 _<OwnerU>_<TableU>.<FieldU>
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                                                , _<OwnerU>_<TableU>.<FieldU>
    </If Loop>
                                            ));
                return context.SaveChanges() > 0;
            }       
        }

        public static bool Delete(<DRType> <DRField>)
        {
            using (var context = new <Database>DBContext())
            {
                context.<OwnerU>_<TableU>.Remove(context.<OwnerU>_<TableU>.Find(
                                                 <DRField>
                                            ));
                return context.SaveChanges() > 0;
            }       
        }
   
    }
}
